<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Validate;
use \app\common\model\Subject as SubjectModel;
use \app\common\model\SchoolSubjectRelation;

/**
 * 科目表接口
 */
class Subject extends Api
{

    /**
     * 科目列表
     * @var array
     */
    protected $noNeedRight = '*';

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new SubjectModel();
        $this->schoolSubjectRelation = new SchoolSubjectRelation();
    }

    /**
     * 查询科目表
     * @ApiTitle    (查询科目表)
     *  @ApiMethod  (POST)
     *  @ApiParams  (name="school_id", type="number", required=true, description="学校ID")
     *  @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     *  @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     *  @ApiReturn   ({
          'code':'200',
          'msg':'查询成功',
          'time': "",
          'data': $row
        })
     */
    public function subjectIndex()
    {
        $data = input('post.');
        $schoolId = $data['school_id'];
        if (!$schoolId || !Validate::is($schoolId, 'number')) {
            $this->error('参数错误');
        }
        
        $subjectIds = $this->schoolSubjectRelation->dataMemberId(['school_id' => $schoolId], 'subject_id');

        $rows = $this->model
            ->where('id', 'in', $subjectIds)
            ->field('id, subject')
            ->select();
        
        $dis = [
            'id' => ['in', $subjectIds],
        ];
        $row = $this->model->dataList($dis, 'id, subject');
        
        $this->success('查询成功', $row);
    }
}